/**
 * 
 * @param rowNum 行号
 * @param i 以下标当作当前单元格的列号
 * @returns 
 */
const getCellSeat = (rowNum:number,i:number) => {
  return {
    "r": rowNum,
    "c": i,
    "rs": 1,
    "cs": 1
  }
}

/**
 * 
 * @param rowNum 行号
 * @param cells 当前行的单元格集合
 * @returns 
 */
const getCells = (rowNum:number,cells:any[]) => {
  // return cells.reduce((pre, cur) => {
  //   pre.push({
  //     "s": {
  //       ...cur
  //     },
  //     "v": cur.v,
  //     "t": cur.ct.t,
  //     "mc": cur.mc
  //   })
  //   return pre
  // }, [])
  let CurrentCells:any[] = []

  for (let i = 0; i < cells.length; i++) {
    if (!cells[i]) {
      break;
    }
    CurrentCells.push({
      "s": {
        ...cells[i]
      },
      "v": cells[i].v,
      "t": cells[i].ct.t,
      // "r": cells[i].mc
      "r": cells[i].mc ? cells[i].mc : getCellSeat(rowNum,i)
    })
  }

  return CurrentCells
}

/**
 * 
 * @param {*} tableRows 行数据集合 array
 * @param {*} rowsLen 提取多少行数据
 * @returns 
 */
const toTableLineData = (tableRows:any[], rowsLen?:number) => {
  let rows:any[] = []
  let oriLen = tableRows.length
  let len = !rowsLen ? oriLen : (oriLen > rowsLen ? rowsLen : oriLen)
  for (let i = 0; i < len; i++) {
    if (!tableRows[i]) {
      break
    }
    let cells = tableRows[i]
    rows[i] = {
      index: i,
      cells: []
    }
    // break
    rows[i]['cells'] = getCells( i ,cells)
  }

  return rows
}

/**
 * 过滤尾部空行
 * @param sheets 
 * @param type 
 * @returns 
 */
const filterEmptyRow = (sheets:any[],type:number) => {
  let filterSheets = sheets.map(sheet => {
    if(type == 1) {
      sheet['table']['rows'] = sheet['table']['rows'].filter((item,index) => {
        return item['cells'].length && item['cells'].length > 0
      })
      return sheet
    }
    if(type == 2) {
      sheet['rows'] = sheet['rows'].filter((item,index) => {
        return item['cells'].length && item['cells'].length > 0
      })
      return sheet
    }
  })
  return filterSheets
}

/**
 * 
 * @param {*} data sheets对象集合
 * @param {*} fileName 文件名
 * @param {*} type 导入接口类型，不同导入接口类型返回不同格式数据。1-全量导入；2-部分导入
 * @param {*} rowsLen 提取多少行数据
 * @returns 
 */
 export const toSheetsTable = (data:any[],fileName:String,type:number,rowsLen?:number) => {
  let sheets:any[] = []
  let step:number = rowsLen ? rowsLen : 0
  for (let i = 0; i < data.length; i++) {
    const sheetData = data[i]
    sheets[i] = type == 1 ? {
      table: {
        index: i,
        name: sheetData.name,
        fileName:fileName,
        rows: []
      },
      // template: {}
    } : {
        index: i,
        name: sheetData.name,
        fileName:fileName,
        rows: []
    }
    // let tem = sheets[i]['table']['rows']
    let currenSheetData = sheetData.data
    if(currenSheetData) {
      if(type == 1) sheets[i]['table']['rows'] = toTableLineData(currenSheetData,currenSheetData.length)
      if(type == 2) sheets[i]['rows'] = toTableLineData(currenSheetData,step)
    }
  }
  // 过滤尾部空行
  let fileterSheets = filterEmptyRow(sheets,type);
  return fileterSheets
}
